/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package appmanager.server.appstore;

import java.io.File;
import java.io.FileInputStream;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import sllib.xmltojava.XmlToJavaListAnnotation;
import sllib.xmltojava.XmlToJavaProcessor;
import appmanager.server.appstore.runner.Runner;
import appmanager.server.appstore.runner.UpdateAppListRun;
import appmanager.server.xmlvo.Application;
import cumulusCloud.common.InformationPath;

public class AppStore {
	private static Logger logger = Logger.getLogger(AppStore.class.getName());
	private static AppStore instance = new AppStore();
	private static File appListFile;
	public static AppStore getInstance() {
		return instance;
	}
	
	private List<Application> applist = new ArrayList<Application>();

	private XmlToJavaListAnnotation appListAnnotation = new XmlToJavaListAnnotation() {
		@Override
		public Class<? extends Annotation> annotationType() {
			return XmlToJavaListAnnotation.class;
		}

		@Override
		public Class<Application> value() {
			return Application.class;
		}
	};

	private AppStore() {
		appListFile = new File(InformationPath.getCloudAppsDataPath("application_manager") + File.separatorChar + "applist.xml");
		loadAppFileList(true);
	}

	public List<Application> getApplist() {
		if (isExpired()) {
			loadAppFileList(true);
		}
		
		return applist;
	}

	private boolean isExpired() {
		if (!appListFile.exists()) return true;
		
		return ( (appListFile.lastModified()-(new Date()).getTime()) > 30*60*1000);
	}

	@SuppressWarnings("unchecked")
	public synchronized void loadAppFileList(boolean reloadinvalid) {
		logger.info("Starting loading file function");
		
		// File not exists
		if (!appListFile.exists()) {
			logger.info("The file does not exists");
			applist.clear();
			if (reloadinvalid) updateAppListFile();
			return;
		}
		
		// Parse the XML file
		logger.info("Parsing the file");
		FileInputStream fin = null;
		try {
			fin = new FileInputStream(appListFile);
			applist = XmlToJavaProcessor.parseXml(fin, List.class, appListAnnotation);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fin.close();
			} catch (Exception e) {
			}
		}
		
		// File is too old
		if (reloadinvalid && isExpired()) {
			logger.info("The file is expired and will be updated");
			updateAppListFile();
		}
	}

	public void updateAppListFile() {
		Runner.addRun(new UpdateAppListRun(appListFile));
	}
}
